package top.serms.leetcode;

import java.util.Arrays;

public class Code27_RemoveElement {


    // 返回删除之后的源数组
    public static int removeElementSpeedIndicator(int[] nums, int val) {
        int slow = 0; // 慢指针

        for (int fast = 0; fast < nums.length; fast++) {
            if (nums[fast] != val) {
                nums[slow++] = nums[fast];
            }
        }

        return slow;
    }
    public static int removeElementOptimize(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while(left < right){
            if (nums[left] == val){
                nums[left] =  nums[right-- - 1];
            }else {
                left++;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] arr = {3, 2, 2, 3};
        System.out.println(removeElementOptimize(arr, 3));
        for (int i : arr) {
            System.out.print(i +  " ");
        }
    }

}
